
package com.medranocg.gd.crecerxxi

import com.medranocg.gd.Customer

import coop.bancocredicoop.crecerxxi.server.XStreamSerializer;
import coop.bancocredicoop.crecerxxi.server.exposicion.ResponseTO;
import coop.bancocredicoop.service.core.response.OutputMessage;
import coop.bancocredicoop.service.core.sender.backport.socket.BackportMessageSender;
import coop.bancocredicoop.service.load.ServiceProvider;
import coop.bancocredicoop.service.transaccion.persona.factory.T24MessageSpecFactory;
import coop.bancocredicoop.service.transaccion.persona.parser.spec.ListaCandidatosMessageSpec;
import coop.bancocredicoop.service.transaccion.persona.specParser.ListaCandidatosTextBeanImpl;

class CrecerXXI {

	def findCustomerByIdPersona(String tipoPersona, Long idPersona) {

		ResponseTO response = null;

		def spec = new T24MessageSpecFactory().getListasCandidatos()
		
		spec.withTipoPersona(tipoPersona);
		spec.withIdPersona(new coop.bancocredicoop.common.domain.numeric.Numeric(idPersona))

		spec.setParseable(false);

		BackportMessageSender service = (BackportMessageSender) ServiceProvider
				.getInstance().lookupService(BackportMessageSender.class);

		OutputMessage output = service.send(spec);

		response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
				(String) output.getData());

	   if (response.data) {
		   List<ListaCandidatosTextBeanImpl> candidatos = (List<ListaCandidatosTextBeanImpl>) response.dataFromXML();

		   candidatos.collect {
			   new CrecerXXICustomer(it.idPersona.longValue() as Long, it.apellido, it.nroInscripcion)
		   }
	   }
		
    }

    def findCustomerByCuit(String tipoPersona, String cuit) {
     	ResponseTO response = null;

		def spec = new T24MessageSpecFactory().getListasCandidatos()
		
		spec.withTipoPersona(tipoPersona);
		spec.withNroInscripcion(cuit);

		spec.setParseable(false);

		BackportMessageSender service = (BackportMessageSender) ServiceProvider
				.getInstance().lookupService(BackportMessageSender.class);

		OutputMessage output = service.send(spec);

		response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
				(String) output.getData());

	   if (response.data) {
		   List<ListaCandidatosTextBeanImpl> candidatos = (List<ListaCandidatosTextBeanImpl>) response.dataFromXML();

		   candidatos.collect {
			   new CrecerXXICustomer(it.idPersona.longValue() as Long, it.apellido, it.nroInscripcion)
		   }
	   }

    }

	def findCustomerByNombre(String tipoPersona, String nombre) {
		ResponseTO response = null;

	   def spec = new T24MessageSpecFactory().getListasCandidatos()
	   
	   spec.withTipoPersona(tipoPersona);
	   spec.withApellido(nombre)

	   spec.setParseable(false);

	   BackportMessageSender service = (BackportMessageSender) ServiceProvider
			   .getInstance().lookupService(BackportMessageSender.class);

	   OutputMessage output = service.send(spec);

	   response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
			   (String) output.getData());

	   if (response.data) {
		   List<ListaCandidatosTextBeanImpl> candidatos = (List<ListaCandidatosTextBeanImpl>) response.dataFromXML();

		   candidatos.collect {
			   new CrecerXXICustomer(it.idPersona.longValue() as Long, it.apellido, it.nroInscripcion)
		   }
	   }

   }
	
    def findOrCreateCustomer(Long idPersona) {
        def customer = Customer.findByIdPersona(idPersona)
        
        if (customer == null) {
            def customerProperties = findCustomerByIdPersona('A', idPersona)[0]
            def newCustomer = new Customer(
                         idPersona: customerProperties.internalId,
                         cuit: customerProperties.cuit,
                         name: customerProperties.name)
            newCustomer.save(flush: true, failOnError: true)
            return newCustomer
        }

        customer

    }

}

